var member_id = '',ws = goods_id = order_id = ''; 
document.addEventListener('plusready',function(){
	member_id = plus.storage.getItem('id');
	ws = plus.webview.currentWebview();
	goods_id = ws.goodsid;
	var store_id = ws.store_id;
//	console.log(apiRoot+"?act=goods&op=index&goods_id="+goods_id);
	plus.nativeUI.showWaiting('加载中...');
	$.ajax({ 
		type:"get", 
		url:apiRoot+"?act=goods&op=index&goods_id="+goods_id,
		dataType:'json', 
		success:function(data){ 
			console.log(JSON.stringify(data));
			if(data){
				var Percentage = parseFloat(data.goods_info.goods_salenum / data.goods_info.goods_storage * 100).toFixed(0);
				console.log(Percentage);
				$('.tel').attr('data-tel',data.goods_info.contacts_phone);
				$('.mui-ellipsis-2').text(data.goods_info.goods_name);
				$('.mui-h4').text(data.goods_info.goods_price);
				$('.Percentage').text('已售'+ Percentage +'%');
				$('.Percentages').val(Percentage);
				$('.goods_marketprice').text('￥' + data.goods_info.goods_marketprice); 
				$('.goods_salenum').text('已抢'+ data.goods_info.goods_salenum +'件'); 
				$('.mui-media-object').attr('src', (pics + data.goods_info.store_id + '/' + data.goods_info.goods_image));			//图片 
				$('.price').text(data.goods_info.goods_price);			//价格
				$('.storage').text(data.goods_info.goods_storage);    //库存
				$('.shop').html(data.goods_info.mobile_body);
				$('.evaluate').text('评价('+data.goods_info.evaluation_count+')');
				$('.goods_id').attr('data-goods_id',data.goods_info.goods_id);
				$('.goods_id').attr('data-store_id',data.goods_info.store_id);
				$('.evaluate').attr('data-goods_id',data.goods_info.goods_id);
				if(data.goods_image_mobile){
					var html = '<div class="mui-slider-item mui-slider-item-duplicate">'+
									'<a href="#">'+
										'<img src="'+ data.goods_image_mobile[0] +'" data-preview-src="" data-preview-group="1"/>'+
									'</a>'+
								'</div>';
					$.each(data.goods_image_mobile, function(k,v) {
						html += '<div class="mui-slider-item">'+
									'<a href="#">'+
										'<img src="'+ v +'" data-preview-src="" data-preview-group="1"/>'+
									'</a>'+
								'</div>';
					});
					$('.mui-slider-loop').html(html);
				}
				if(data.goods_info.spec_name){
					var spec = '';
					$.each(data.goods_info.spec_name, function(k1,v1) {
						spec += '<div><span>'+ v1 +'</span><p class="chose m5">';
						var num = 0;
						$.each(data.goods_info.spec_value[k1], function(k2,v2) {
							if(num == 0){
								spec += '<button type="button" class="mui-btn back-orange backs" data-specid="'+ k1 +'" data-id="'+ k2 +'">'+ v2 +'</button>';
								num += 1;
							}else{
								spec += '<button type="button" class="mui-btn backs" data-specid="'+ k1 +'" data-id="'+ k2 +'">'+ v2 +'</button>';
							}
							
						});
						spec += '</p></div>';
					});
					$('.spec_value').html(spec);
				} 
				 
				$(".chose>button").on('click',function(){
		            var $this = $(this);
		            $this.on('click',function(){
		                $this.siblings().removeClass('back-orange');
		                $this.addClass('back-orange');
		                var sids = selected = '';
						$('.back-orange').each(function(){
							selected += $(this).text() + ",";
							sids += $(this).attr('data-id') + ",";
					    });
					    var sids = sids.substring(0,(sids.length - 1));
					    var sid_s = sids.split(',');
					    spec1 = sid_s.sort(function(a,b){
					        return a-b;
					    });
					    var spec_sign = spec1.join('|');
					    $('.selected').text(selected);
					    var good_id = data.spec_list_mobile[spec_sign];
//					    console.log(apiRoot+"?act=goods&op=index&goods_id="+good_id);
					    $.ajax({
					    	type:"get",
							url:apiRoot+"?act=goods&op=index&goods_id="+good_id,
							dataType:'json',
							success:function(info){
								console.log(JSON.stringify(info.goods_info.goods_id));
								if(info){
									$('.price').text(info.goods_info.goods_price);			//价格
									$('.storage').text(info.goods_info.goods_storage);    //库存
									$('.goods_id').attr('data-goods_id',info.goods_info.goods_id);
									
								}
							},
							error:function(e){
								toast('加载规格失败！');
							}
					    });
		            });
		        });
			}
			plus.nativeUI.closeWaiting();
		}, 
		error:function(e){
//			console.log(JSON.stringify(e));
			plus.nativeUI.closeWaiting();
			error();
		}
	});
},false); 
function goods_evaluation(){
	var goods_id = $('.evaluate').attr('data-goods_id');
	var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
	var webviewContent= plus.webview.create('./goods_evaluation.html','goods_evaluation.html',{},{goods_id:goods_id});//后台创建webview并打开show.html
	webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
        nwaiting.close(); //新webview的载入完毕后关闭等待框
        webviewContent.show("slide-in-right",500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
    }, false);
}
